<template>
	<view class="homeLayout pageBg">
		<post-nav-bar title="发布"></post-nav-bar>


		<view class="item">
			<view class="box" v-for="(dish, index) in dishList" :key="dish.dishId" @click="goPost(dish.dishId)">
				<view class="item-content">
					<image :src="getImage2(dish.imageUrl)" mode="aspectFill" class="thumbnail" />
					<view class="count-info">
						<view class="counts">
							<uni-icons type="eye" color="#ffffff" size="18" />
							<text>{{ format(dish.views) }}</text>
						</view>
						<view class="counts">
							<uni-icons type="hand-up" color="#ffffff" size="18" />
							<text>{{ format(dish.likes) }}</text>
						</view>
						<view class="counts">
							<uni-icons type="star" color="#ffffff" size="18" />
							<text>{{ format(dish.favorites) }}</text>
						</view>
					</view>
					<view class="item-info">
						<view class="title">
							<up-text :lines="2" :text="dish.name" size="16" />
						</view>
						<view class="user">
							<uni-icons type="person" color="#888" size="18" />
							<text>{{ dish.username }}</text>
						</view>
					</view>
				</view>
			</view>
		</view>

		<up-back-top :scroll-top="scrollTop" :iconStyle="iconStyle" :customStyle="customStyle" />

	</view>
</template>

<script setup>
	import {
		ref,
		onMounted
	} from 'vue';
	import {
		appUrl
	} from '../../main';
	import {
		onPageScroll
	} from '@dcloudio/uni-app';

	// 响应式数据
	const dishList = ref([]);
	const scrollTop = ref(0);
	const iconStyle = ref({
		color: '#ffffff'
	});
	const customStyle = ref({
		background: '#FF4D4F'
	});

	const format = (num) => {
		if (num >= 100000) {
			return Math.round(num / 10000) + "w"
		} else if (num >= 10000) {
			return (Math.round(num / 1000)) / 10 + "w"
		} else {
			return num
		}
	}

	// 图片路径处理
	const getImage2 = (path) => {
		return `https://xiaoxiaobaiyang.oss-cn-shenzhen.aliyuncs.com/food_moment_discover/train/data/${path}`;
	};

	// 跳转详情页
	const goPost = (id) => {
		uni.setStorageSync('dishId', id)
		uni.navigateTo({
			url: "/pages/PostView/PostView"
		})
	}

	// 获取数据
	const fetchDishList = async () => {
		try {
			const followId = uni.getStorageSync("followId");
			const res = await uni.request({
				url: `${appUrl}/dish/${followId}`,
				method: "POST",
				data: {
					page: 1,
					limit: 20
				}
			});

			if (res.data.status === 200) {
				dishList.value = res.data.data.dishes;
			} else {
				uni.showToast({
					title: '数据加载失败',
					icon: 'none'
				});
			}
		} catch (error) {
			console.error('请求失败:', error);
			uni.showToast({
				title: '网络异常',
				icon: 'none'
			});
		}
	};

	// 生命周期
	onMounted(fetchDishList);
	onPageScroll((e) => {
		scrollTop.value = e.scrollTop;
	});
</script>

<style lang="scss" scoped>
	.homeLayout {
		height: 120vh;

		.banner {
			width: 750rpx;
			padding: 30rpx 0;

			swiper {
				width: 750rpx;
				height: 340rpx;

				&-item {
					width: 100%;
					height: 100%;
					padding: 0 30rpx;

					image {
						width: 100%;
						height: 100%;
						border-radius: 10rpx;
					}
				}
			}
		}

		.item {
			margin-top: 30rpx;
			padding: 0 30rpx;
			display: grid;
			grid-template-columns: 1fr 1fr;
			column-gap: 20rpx;
			row-gap: 20rpx;

			.box {
				height: 480rpx;
				border-radius: 12rpx;
				overflow: hidden;
				position: relative;
				background-color: #FFFFFF;

				.count-info {
					position: absolute;
					bottom: 155rpx;
					left: 0;
					display: flex;
					align-items: center;
					padding: 10rpx;
					background-color: rgba(0, 0, 0, 0);
					color: white;
					font-size: 13px;
					gap: 18rpx;

					.counts {
						display: flex;
						gap: 4rpx;
						align-items: center;
					}
				}

				.thumbnail {
					width: 100%;
					height: 320rpx;
				}

				.item-info {
					margin-left: 10rpx;

					.title {
						font-size: 16px;
						color: #333;
						height: 100rpx;
					}

					.user {
						display: flex;
						font-size: 14px;
						color: #999;
						gap: 10rpx;
						align-items: center;
					}
				}
			}
		}
	}
</style>